﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PCMSofCC.BLL;

namespace PCMSofCC.GroupLeader
{
    public partial class ReviewRecordStep2 : System.Web.UI.Page
    {
        private int RecordId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
            {
                Calendar Calendar1 = (Calendar)PreviousPage.Master.FindControl("Main").FindControl("Calendar1");
                DateTime date = Calendar1.SelectedDate;
                ListBox SiteList = (ListBox)PreviousPage.Master.FindControl("Main").FindControl("SiteList");
                int siteId = int.Parse(SiteList.SelectedValue);
                var record = Records.GetRecord(siteId, date);
                this.RecordId = record.RecordId;
                ViewState["RecordId"] = this.RecordId;
                tbxRemark.Text = record.Remark;
                if (record.HasPassed.HasValue)
                    rblHasPassed.SelectedValue = (record.HasPassed == 1) ? "是" : "否";
                else
                    rblHasPassed.SelectedIndex = -1;
                LinqDataSource1.WhereParameters["RecordId"] = new Parameter("RecordId", TypeCode.Int32, record.RecordId.ToString());
            }
            else if (!IsPostBack)
                Response.Redirect("ReviewRecordStep1.aspx");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            foreach (GridViewRow row in gvCompleteRecord.Rows)
            {
                RecordEntry entry = db.RecordEntry.Single(x => x.Id == (int)gvCompleteRecord.DataKeys[row.RowIndex].Value);
                entry.RemarkByGroupLeader = ((TextBox)row.FindControl("tbxRemarkByGroupLeader")).Text;
            }
            this.RecordId = (int)ViewState["RecordId"];
            var record = Records.GetRecord(this.RecordId);
            record.HasPassed = (rblHasPassed.SelectedValue == "是") ? 1 : 0;
            record.Remark = tbxRemark.Text;
            db.SubmitChanges();
        }
    }
}
